package studentsinfomanagement.view.teacher;
import studentsinfomanagement.bean.Course;
import studentsinfomanagement.bean.Teacher;
import studentsinfomanagement.dao.CourseDAO;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 * 老师选课中心界面类
 * @author
 * @Time
 */
public class TeacherCourseCenter extends JFrame {
    private TeacherCourseCenter teacherCourseCenter = null;
    private Teacher teacher = null;//老师信息对象
    private DefaultTableModel tableModel;

    public TeacherCourseCenter(Teacher teacher){
        teacherCourseCenter = this;
        this.teacher = teacher;
        this.setTitle("教师选课中心");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //课程滚动显示区域
        String[] columnNames = null;  // 列名
        String [][]tableVales= null; //数据
        showMyCourses(columnNames,tableVales);//获取全部授课信息
        JTable table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);

        //退出选课中心
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);

        //添加监听器
        //行选择监听器，教师选择行可以查看对应课程的学生选课信息
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int sel_row = table.getSelectedRow();
                String cno = (String)table.getValueAt(sel_row,0);
                Course course = new Course();
                course.setCno(cno);
                course.setTno(teacher.getTno());
                new SCTeacherView(course);
            }
        });

        //退出
        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                teacherCourseCenter.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    /**
     * 展示教师的授课课程信息
     * @param columnNames 列名
     * @param tableVales 二位数组内容
     */
    public void showMyCourses(String[] columnNames,String [][]tableVales){
        columnNames = new String[]{"课程号","课程名","学分"};
        CourseDAO courseDAO = CourseDAO.getInstance();
        List<Course> courseList = courseDAO.getCoursesByTno(teacher.getTno());
        tableVales = new String[courseList.size()][];
        int i = 0;
        for(Course c:courseList){
            tableVales[i] = new String[3];
            tableVales[i][0] = c.getCno();
            tableVales[i][1] = c.getName();
            Integer score = c.getScore();
            tableVales[i][2] = score.toString();
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
    }
}
